﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private double getPacket(ref double total, int n)
        {
            if (n == 1) return total;
            Random rnd = new Random();
            double avg = total / n / 2;
            double thisPacket = avg + avg * rnd.Next(101) / 100;
            thisPacket = Math.Round(thisPacket, 2);
            total -= thisPacket;
            total = Math.Round(total, 2);
            return thisPacket;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double total = int.Parse(textBox1.Text);
            int n = int.Parse(textBox2.Text);
            double[] redPacket = new double[n];

            for (int i = n; i > 0; i--)
            {
                redPacket[i - 1] = getPacket(ref total, i);
            }

            textBox3.Text = string.Join("\t", redPacket);
            textBox4.Text = sumPacket(redPacket).ToString();
        }

        private double sumPacket(double[] a)
        {
            double sum = 0;
            foreach (double m in a)
                sum += m;
            return sum;
        }
    }
}
